package com.shen.accountbook.module.home.accountfragment;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shen.accountbook.R;
import com.shen.accountbook.bean.AccountBean;
import com.shen.accountbook.module.base.EmptyPresenterImpl;
import com.shen.accountbook.module.base.EmptyView;
import com.shen.accountbook.module.base.MVPFragment;
import com.shen.accountbook.utils.DateUtils;
import com.shen.accountbook.utils.TestDataUtil;

import java.util.Date;
import java.util.List;

import butterknife.BindView;

import static com.shen.accountbook.config.Constant.colorRes;
import static com.shen.accountbook.config.Constant.distance;

/**
 * author:  shen
 * date:    2018/8/27
 */
public class AccountFragment extends MVPFragment<EmptyPresenterImpl> implements EmptyView {

    @BindView(R.id.layout_data_top)
    LinearLayout mLayoutDataTop;

    /** 年份 */
    @BindView(R.id.tv_year_top)
    TextView mTvYearTop;
    /** 月份 */
    @BindView(R.id.tv_month_top)
    TextView mTvMonthTop;
    /** 结余(元) */
    @BindView(R.id.tv_cash_surplus_top)
    TextView mTvCashSurplusTop;
    /** 相比上月收入 */
    @BindView(R.id.tv_cash_compared_top)
    TextView mTvCashComparedTop;


    @BindView(R.id.rv_list_accountfragment)
    RecyclerView mRvList;
    @BindView(R.id.swipe_accountfragment)
    SwipeRefreshLayout mSwipe;


    private AccountAdapter mAdapter;
    private List<AccountBean.ListBean> mList;


    @Override
    protected EmptyPresenterImpl createPresent(Context context) {
        return new EmptyPresenterImpl(context, this);
    }

    @Override
    public int getResId() {
        return R.layout.fragment_account;
    }

    @Override
    protected void initData() {
        setTopView();
        setSwipe();
        setRecyclerView();
    }

    @Override
    protected void initEvent() {
        setTestData();
    }


    private void setTopView(){
        mTvYearTop.setText(DateUtils.getCurYear("yyyy 年"));
        mTvMonthTop.setText(DateUtils.date2Str(new Date(), "MM"));
    }


    private void setSwipe(){
        mSwipe.setColorSchemeColors(colorRes, colorRes);             // 改变加载显示的颜色
        mSwipe.setDistanceToTriggerSync(distance);                   // 设置向下拉多少出现刷新
        mSwipe.setProgressViewEndTarget(false, distance);        // 设置刷新出现的位置
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipe.setRefreshing(false);
            }
        });
    }

    private void setRecyclerView(){
        mRvList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new AccountAdapter(mContext, mList);
        mAdapter.setmListener(new AccountAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
            }
        });
        mRvList.setAdapter(mAdapter);
    }


    private void setTestData() {
        AccountBean data = TestDataUtil.getAccountBean();
        mList = data.getList();
        mAdapter.setmDatas(mList);
        mAdapter.notifyDataSetChanged();
    }

}
